package ch.fhnw.qualiscan.application.modals;

import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.EmailHelper;
import ch.fhnw.qualiscan.core.libraries.ModalLibrary;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class ForgotPassword extends Modal {
	
	VerticalLayout wrap;
	HorizontalLayout form;
	UserModel um;
	
	TextField userEmail;
	Label status;
	
	public ForgotPassword() {
		
		wrap = new VerticalLayout();
		wrap.setSpacing(true);
		
		setCaption(TM.get("pwdforgot.title"));
		wrap.addComponent(textHelper.p(TM.get("pwdforgot.desc")));
		
		status = new Label("");
		status.setContentMode(Label.CONTENT_XHTML);
		
		// if user has already tried to login
		form = new HorizontalLayout();
		form.setSpacing(true);
		
		// 1 column
		Label eMail = new Label(TM.get("user.email"));
		eMail.setWidth("100px");
		
		// 2 column
		userEmail = new TextField();
		userEmail.setInputPrompt(TM.get("user.email"));
		userEmail.setWidth("200px");
		userEmail.addValidator(new EmailValidator(TM.get("user.error.emailinvalid")));
		
		// 3 column
		Button sendPwd = new Button(TM.get("pwdforgot.sendpwd"));
		sendPwd.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				try {
					userEmail.validate();
					if(userEmail.isValid()) {
						userExists(userEmail.getValue().toString());
					} else {
						status.setValue(TM.get("pwdforgot.error.invalid"));
					}
				}
				catch(InvalidValueException e) {
					
				}
			}
		});
		
		form.addComponent(eMail);
		form.addComponent(userEmail);
		form.addComponent(sendPwd);
		
		wrap.addComponent(form);
		wrap.addComponent(status);
		
		addComponent(wrap);
	} 

	
	public boolean userExists(String email) {
		if(email.equals("")) {
			
		} else {
			um = new UserModel();
			User u = um.getUserByEmail(email);
			
			if(u != null)
				editPassword(u);
			else
				status.setValue(TM.get("pwdforgot.error.exists"));
		}
		
		return true;
	}
	
	public void editPassword(User u) {
		// for userfriendlyness
		userEmail.setValue(u.getEmail());
		
		// generate password
		String newPwd = Long.toHexString(Double.doubleToLongBits(Math.random()));
		
		// add to user
		u.setPassword(newPwd);
		
		// send password
		EmailHelper email = new EmailHelper();
		email.sendPasswordMail(u);
		
		// save user
		um.updateUser(u);
		
		showSuccess(u.getEmail());
	}
	
	public void showSuccess(String email) {
		form.setEnabled(false);
		
		Button closeButton = new Button(TM.get("action.close"));
		closeButton.addListener(new Button.ClickListener() {	
			public void buttonClick(ClickEvent event) {
				closeModal();	
			}
		});
		
		status.setValue(TM.get("pwdforgot.success", email));
		wrap.addComponent(closeButton);
	}
	
}
